﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PacMan
{
    /// <summary>
    /// Provides the logic for character named Blinky.
    /// </summary>
    class Blinky : Character
    {
        #region Private Fields
        private int _distance = 0;
        #endregion

        #region Public Constructors
        public Blinky(int x, int y, int speed)
            : base(x, y, speed)
        {
            this.Width = 40;
            this.Height = 40;
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Makes Blinky to move over maze.
        /// </summary>
        public void Move()
        {
            WhereToMove();

            if (Position == Position.Down)
            {
                Y += Speed;
                if (Y > Environment.MaxY - Width)
                {
                    Y = Environment.MinY;
                }
                return;
            }
            if (Position == Position.Right)
            {
                X += Speed;
                if (X > Environment.MaxX - Width)
                {
                    X = Environment.MinX;
                }
                return;
            }
            if (Position == Position.Up)
            {
                Y -= Speed;
                if (Y < Environment.MinY)
                {
                    Y = Environment.MaxY - Width;
                }
                return;
            }
            if (Position == Position.Left)
            {
                X -= Speed;
                if (X < Environment.MinX)
                {
                    X = Environment.MaxX - Width;
                }
                return;
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        ///     Defines the position where should Blinky move.
        /// </summary>
        private void WhereToMove()
        {

            if (_distance < 30)
            {
                Random rand = new Random();
                int choosePosition = rand.Next(0, 5);

                switch (choosePosition)
                {
                    case 1:
                        {
                            Position = Position.Left;
                            break;
                        }
                    case 2:
                        {
                            Position = Position.Right;
                            break;
                        }
                    case 3:
                        {
                            Position = Position.Up;
                            break;
                        }
                    case 4:
                        {
                            Position = Position.Down;
                            break;
                        }
                }
                _distance = rand.Next(1, 60);
            }
            _distance--;
        }
        #endregion
    }
}
